﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Data;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace OpenSunTracker.RestClient.Extensions
{
    public static class StreamExtensions
    {


        public static string ReadAllText(this WebResponse response)
        {
            Stream stream = response.GetResponseStream();

            Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

            // Pipe the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader(stream, encode);

            Char[] read = new Char[256];

            // Read 256 charcters at a time.     
            int count = readStream.Read(read, 0, 256);
            var strContent = new StringBuilder();

            while (count > 0)
            {
                // Dump the 256 characters on a string and display the string onto the console.
                String str = new String(read, 0, count);
                strContent.Append(str);
                count = readStream.Read(read, 0, 256);
            }

            Console.WriteLine("");
            // Release the resources of stream object.
            readStream.Close();

            // Release the resources of response object.
            response.Close();

            return strContent.ToString();
        }
        

    }
}
